package com.ebupt.migu.music.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ebupt.migu.common.dto.MiguSubCompanyCampDto;
import com.ebupt.migu.music.common.auth.AuthBaseUtils;
import com.ebupt.migu.music.common.constants.RoleConstants;
import com.ebupt.migu.music.common.constants.StrategyConstants;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.MyException;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.music.common.sms.Sender;
import com.ebupt.migu.music.common.util.*;
import com.ebupt.migu.music.market.dto.MarketSmsAuditRecordDTO;
import com.ebupt.migu.music.market.dto.MarketSmsCountDTO;
import com.ebupt.migu.music.market.dto.MarketSmsStrategyDTO;
import com.ebupt.migu.music.market.dto.MarketSmsStrategySAuditDTO;
import com.ebupt.migu.music.market.entity.*;
import com.ebupt.migu.music.market.mapper.*;
import com.ebupt.migu.music.market.service.MarketSmsStrategyService;
import com.ebupt.migu.music.market.vo.MarketSmsAuditPageVO;
import com.ebupt.migu.music.market.vo.MarketSmsCountPageVO;
import com.ebupt.migu.music.market.vo.MarketSmsStrategyPageVO;
import com.ebupt.migu.music.market.vo.MarketSmsStrategyVO;
import com.ebupt.migu.music.megamarket.mapper.MegamarketMapper;
import com.ebupt.migu.music.system.entity.SysUser;
import com.ebupt.migu.music.system.mapper.RoleMapper;
import com.ebupt.migu.music.unifyAudit.mapper.UnifyAuditMapper;
import com.ebupt.migu.music.unifyAudit.pojo.AuditData;
import com.ebupt.migu.music.unifyAudit.pojo.AuditStatus;
import com.ebupt.migu.music.unifyAudit.pojo.UnifyAudit;
import com.ebupt.migu.music.unifyAudit.service.UnifyAuditService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 23265
 * @description 针对表【market_sms_strategy(运营短信策略)】的数据库操作Service实现
 * @createDate 2024-09-10 10:46:24
 */
@Slf4j
@Service
public class MarketSmsStrategyServiceImpl extends ServiceImpl<MarketSmsStrategyMapper, MarketSmsStrategy>
        implements MarketSmsStrategyService {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private MarketSmsAuditMapper marketSmsAuditMapper;

    @Autowired
    private MegamarketMapper megamarketMapper;

    @Autowired
    private MarketSmsStrategyMapper marketSmsStrategyMapper;

    @Autowired
    private MarketSmsInfoMapper marketSmsInfoMapper;

    @Autowired
    private MarketSmsRevocationRecordMapper marketSmsRevocationRecordMapper;

    @Resource
    private UnifyAuditService unifyAuditService;

    @Resource
    private UnifyAuditMapper unifyAuditMapper;

    @Autowired
    private MarketSmsAuditRecordMapper marketSmsAuditRecordMapper;

    @Autowired
    private MarketSmsCountMapper marketSmsCountMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Value("${megamarket.ip}")
    private String MEGAMARKET_IP;

    @Value("${megamarket.subCompanyCampSync2Marketing}")
    private String MEGAMARKET_SUBCOMPANYCAMPSYNC2MARKETING;
    @Autowired
    private MarketSmsAuditRecordServiceImpl marketSmsAuditRecordServiceImpl;

    @Override
    public ResultObject addStrategy(MarketSmsStrategyVO strategyVO) {
        boolean b = true;
        ResultObject res = new ResultObject();
        String strategyName = null;
        try {
            strategyVO.setStrategyId(getStrategyId());
            // 策略名称不可为空
            if (null == strategyVO.getStrategyName()) {
                throw new MyException(StatusEnum.BUSINID, "策略名称不可为空");
            }
            strategyName = strategyVO.getStrategyName();
            // 策略名称不可重复
            Map<String, Object> strMap = new HashMap<>(1);
            strMap.put("strategyName", strategyName);
            boolean isExist = this.baseMapper.exists(new QueryWrapper<MarketSmsStrategy>().lambda().eq(true,
                    MarketSmsStrategy::getStrategyName, strategyName));
            if (isExist) {
                throw new MyException(StatusEnum.STRATEGY_NAME_EXIST, strategyName + "的策略名称已存在");
            }
            Map<String, Object> params = AuthBaseUtils.authBaseInfo(null, request);
            strategyVO.setCreatorId(params.get("creatorId").toString());
            strategyVO.setCreatorName(params.get("creatorName").toString());
            strategyVO.setCreatorDepartment(params.get("creatorDepartment").toString());
            strategyVO.setCreatorPhone(params.get("creatorPhone").toString());
            strategyVO.setCreateTime(new Date());
            // 创建market_sms_strategy表记录
            MarketSmsStrategy marketSmsStrategy = new MarketSmsStrategy();
            BeanUtils.copyProperties(strategyVO, marketSmsStrategy);
            // 解析上传文件
            parseRingtoneIdFile(marketSmsStrategy);
            marketSmsStrategy.setExecuteStatus(1);
            marketSmsStrategy.setApprovalStatus(1);

            // 创建market_sms_info表记录
            MarketSmsInfo marketSmsInfo = strategyVO.getMarketSmsInfo();
            marketSmsInfo.setStrategyId(strategyVO.getStrategyId());
            // 构造market_sms_audit实体
            MarketSmsAudit marketSmsAudit = strategyVO.getMarketSmsAudit();
            marketSmsAudit.setStrategyId(strategyVO.getStrategyId());

            // 内容审核
            if (strategyVO.getAuditType() == 1) {
                // 短信内容审核
                smsUnifyAudit(marketSmsInfo);
                if (strategyVO.getSendNum() < 200) {
                    b = false;
                    marketSmsStrategy.setExecuteStatus(1);
                    marketSmsStrategy.setApprovalStatus(3);
                    marketSmsStrategy.setStartTime(TenMinutes(LocalDateTime.now()));
                    marketSmsStrategy.setEndTime(OneYear(LocalDateTime.now()));
                    this.save(marketSmsStrategy);
                    sendToMegamarketAudit(marketSmsStrategy, marketSmsInfo);
                } else {
                    // 本系统三级审核人等待内容审核平台通过后进入审核状态
                    marketSmsAudit.setLevel1Status(3);
                    marketSmsAudit.setLevel2Status(3);
                    marketSmsAudit.setLevel3Status(3);
                }
            }
            // 在免审核情况下，如果预计运营规模大于200人，需要系统审核，且审核状态为待审核
            if (strategyVO.getAuditType() == 2) {
                // 第一级审核人进入待审核状态，第二级与第三级审核人等待审核流转
                marketSmsAudit.setLevel1Status(0);
                marketSmsAudit.setLevel2Status(3);
                marketSmsAudit.setLevel3Status(3);
            }
            // 当客群规模大于200人时，需要完成本系统审核流程
            if (strategyVO.getSendNum() > 200) {
                marketSmsStrategy.setExecuteStatus(1);
                marketSmsStrategy.setApprovalStatus(1);
                marketSmsAuditMapper.insert(marketSmsAudit);
            }
            // 仅当不需要内容审核且客群规模小于200时，才会在创建过程中向大营销平台发起审核流程
            if (strategyVO.getAuditType() == 2 && strategyVO.getSendNum() < 200) {
                b = false;
                marketSmsStrategy.setExecuteStatus(1);
                marketSmsStrategy.setApprovalStatus(3);
                marketSmsStrategy.setStartTime(TenMinutes(LocalDateTime.now()));
                marketSmsStrategy.setEndTime(OneYear(LocalDateTime.now()));
                this.save(marketSmsStrategy);
                sendToMegamarketAudit(marketSmsStrategy, marketSmsInfo);

            }

            if (b) {
                this.save(marketSmsStrategy);
            }
            marketSmsInfoMapper.insert(marketSmsInfo);
            res.setMsg("创建策略成功！");
            return res;
        } catch (MyException e) {
            log.error(strategyName + "的创建策略相应参数验证失败", e);
            throw e;
        } catch (Exception e) {
            log.error(strategyName + "的创建策略失败", e);
            throw new MyException(StatusEnum.ERROR, strategyName + "的创建策略失败");
        }
    }

    /**
     * 解析铃声ID批量上传文件
     *
     * @param marketSmsStrategy
     */
    private void parseRingtoneIdFile(MarketSmsStrategy marketSmsStrategy) {
        List<String[]> data = CSVUtils.csvRead(marketSmsStrategy.getRingtoneIdFilePath());
        String ringtonesIds = null;
        if (null != data && data.size() > 0) {
            for (int i = 1; i < data.size(); i++) {//跳过首行
                if (StringUtils.isNotEmpty(ringtonesIds)) {
                    ringtonesIds = data.get(i)[0];
                } else {
                    ringtonesIds = ringtonesIds + "," + data.get(i)[0];
                }
            }
        }
        marketSmsStrategy.setRingtoneId(ringtonesIds);
    }

    @Override
    public ResultObject strategyList(MarketSmsStrategyPageVO vo) {
        ResultObject<Page<MarketSmsStrategy>> res = new ResultObject<>();
        try {
            SysUser user = AuthBaseUtils.getSessionUser(request);
            // 超级管理员可以查询所有运营策略， 非超级管理员仅能查询本部门运营策略
            String userID = user.getId().toString();
            List<String> roles = roleMapper.queryRoleIdByUserId(userID);
            if (!roles.contains(RoleConstants.SUPER_ADMIN) && !roles.contains(RoleConstants.SUPERAIMIN)) {
                // 如果不是系统管理员或超级管理员， 设置查询范围为本部门
                vo.setCreatorDepartment(user.getDeptName());
            }
            QueryWrapper<MarketSmsStrategy> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(FuncUtil.isNotEmpty(vo.getCreatorDepartment()), MarketSmsStrategy::getCreatorDepartment,
                            vo.getCreatorDepartment())
                    .like(FuncUtil.isNotEmpty(vo.getCreatorName()), MarketSmsStrategy::getCreatorName, vo.getCreatorName())
                    .like(FuncUtil.isNotEmpty(vo.getStrategyName()), MarketSmsStrategy::getStrategyName,
                            vo.getStrategyName())
                    .eq(FuncUtil.isNotNull(vo.getExecuteStatus()), MarketSmsStrategy::getExecuteStatus,
                            vo.getExecuteStatus())
                    .eq(FuncUtil.isNotNull(vo.getExecuteType()), MarketSmsStrategy::getExecuteType, vo.getExecuteType())
                    .ge(FuncUtil.isNotEmpty(vo.getSd()), MarketSmsStrategy::getCreateTime, vo.getSd())
                    .le(FuncUtil.isNotEmpty(vo.getEd()), MarketSmsStrategy::getCreateTime, vo.getEd())
                    .orderByDesc(MarketSmsStrategy::getCreateTime);
            // 分页查询结果集
            Long total = marketSmsStrategyMapper.selectCount(queryWrapper);
            Page<MarketSmsStrategy> pageQuery = new Page<>(vo.getPage(), vo.getPageSize());
            IPage<MarketSmsStrategy> result = baseMapper.selectPage(pageQuery, queryWrapper);
            List<MarketSmsStrategySAuditDTO> mapList = result.getRecords().stream().map(marketSmsStrategy -> {
                MarketSmsStrategySAuditDTO marketSmsStrategySAuditDTO = new MarketSmsStrategySAuditDTO();
                BeanUtils.copyProperties(marketSmsStrategy, marketSmsStrategySAuditDTO);
                MarketSmsAudit marketSmsAudit = marketSmsAuditMapper.selectOne(new QueryWrapper<MarketSmsAudit>()
                        .lambda().eq(MarketSmsAudit::getStrategyId, marketSmsStrategy.getStrategyId()));
                marketSmsStrategySAuditDTO.setMarketSmsAudit(marketSmsAudit);
                if (marketSmsStrategy.getExecuteType() == 1 && marketSmsStrategy.getSendNum() > 200) {
                    if (!FuncUtil.isEmpty(marketSmsAudit)) {
                        // 0短信审核
                        if (marketSmsAudit.getLevel1Status() == 3) {
                            marketSmsStrategySAuditDTO.setStatus(0);
                        } else {
                            marketSmsStrategySAuditDTO.setStatus(1);
                        }
                    }
                }
                if (marketSmsStrategy.getExecuteType() == 1 && marketSmsStrategy.getSendNum() < 200) {
                    marketSmsStrategySAuditDTO.setStatus(0);
                }
                return marketSmsStrategySAuditDTO;
            }).collect(Collectors.toList());

            // 分页
            Page page = new Page<>();
            page.setTotal(total);
            page.setRecords(mapList);
            res.setCode(StatusEnum.SUCCESS.getCode());
            res.setMsg(StatusEnum.SUCCESS.getMsg());
            res.setData(page);
            return res;
        } catch (Exception e) {
            log.error("查询策略列表失败", e);
            throw new MyException(StatusEnum.ERROR, "查询策略列表失败");
        }

    }

    @Override
    public ResultObject strategyEdit(MarketSmsStrategyVO vo) {
        boolean b = true;

        ResultObject res = new ResultObject();
        try {

            Map<String, Object> params = AuthBaseUtils.authBaseInfo(null, request);
            vo.setUpId(params.get("creatorId").toString());
            vo.setUpName(params.get("creatorName").toString());
            vo.setUpDepartment(params.get("creatorDepartment").toString());
            vo.setUpTime(new Date());

            // 修改market_sms_strategy表记录
            MarketSmsStrategy marketSmsStrategy = new MarketSmsStrategy();
            BeanUtils.copyProperties(vo, marketSmsStrategy);

            // 修改market_sms_info表记录
            MarketSmsInfo marketSmsInfo = vo.getMarketSmsInfo();
            BeanUtils.copyProperties(vo.getMarketSmsInfo(), marketSmsInfo);
            UpdateWrapper<MarketSmsInfo> marketSmsInfoLambdaUpdateWrapper = new UpdateWrapper<>();
            marketSmsInfoLambdaUpdateWrapper.lambda().eq(MarketSmsInfo::getStrategyId, vo.getStrategyId());
            marketSmsInfoMapper.update(marketSmsInfo, marketSmsInfoLambdaUpdateWrapper);

            UpdateWrapper<MarketSmsAudit> smsAuditUpdateWrapper1 = new UpdateWrapper<>();
            smsAuditUpdateWrapper1.lambda().eq(MarketSmsAudit::getStrategyId, vo.getStrategyId());
            marketSmsAuditMapper.delete(smsAuditUpdateWrapper1);
            // 构造market_sms_audit实体
            MarketSmsAudit marketSmsAudit = vo.getMarketSmsAudit();
            BeanUtils.copyProperties(vo.getMarketSmsAudit(), marketSmsAudit);
            marketSmsAudit.setStrategyId(vo.getStrategyId());

            // 内容审核
            if (vo.getAuditType() == 1) {
                // 短信内容审核
                smsUnifyAudit(marketSmsInfo);
                if (vo.getSendNum() < 200) {
                    b = false;
                    marketSmsStrategy.setExecuteStatus(1);
                    marketSmsStrategy.setApprovalStatus(3);
                    marketSmsStrategy.setStartTime(TenMinutes(LocalDateTime.now()));
                    marketSmsStrategy.setEndTime(OneYear(LocalDateTime.now()));
                    this.update(marketSmsStrategy, new UpdateWrapper<MarketSmsStrategy>().lambda()
                            .eq(MarketSmsStrategy::getStrategyId, vo.getStrategyId()));
                    sendToMegamarketAudit(marketSmsStrategy, marketSmsInfo);

                } else {
                    // 本系统三级审核人等待内容审核平台通过后进入审核状态
                    marketSmsAudit.setLevel1Status(3);
                    marketSmsAudit.setLevel2Status(3);
                    marketSmsAudit.setLevel3Status(3);
                    marketSmsStrategy.setExecuteStatus(1);
                    marketSmsStrategy.setApprovalStatus(1);
                    marketSmsAuditMapper.insert(marketSmsAudit);

                    // 初始化审核记录

                    if (FuncUtil.isNotNull(marketSmsAudit.getLevel1ApprovalId())) {
                        MarketSmsAuditRecord marketSmsAuditRecord = new MarketSmsAuditRecord();
                        marketSmsAuditRecord.setStrategyId(vo.getStrategyId());
                        marketSmsAuditRecord.setApprovalId(marketSmsAudit.getLevel1ApprovalId());
                        marketSmsAuditRecord.setApprovalName(marketSmsAudit.getLevel1ApprovalName());
                        marketSmsAuditRecordMapper.insert(marketSmsAuditRecord);
                    }

                    if (FuncUtil.isNotNull(marketSmsAudit.getLevel2ApprovalId())) {
                        MarketSmsAuditRecord marketSmsAuditRecord = new MarketSmsAuditRecord();
                        marketSmsAuditRecord.setStrategyId(vo.getStrategyId());
                        marketSmsAuditRecord.setApprovalId(marketSmsAudit.getLevel2ApprovalId());
                        marketSmsAuditRecord.setApprovalName(marketSmsAudit.getLevel2ApprovalName());
                        marketSmsAuditRecordMapper.insert(marketSmsAuditRecord);
                    }

                    if (FuncUtil.isNotNull(marketSmsAudit.getLevel3ApprovalId())) {
                        MarketSmsAuditRecord marketSmsAuditRecord = new MarketSmsAuditRecord();
                        marketSmsAuditRecord.setStrategyId(vo.getStrategyId());
                        marketSmsAuditRecord.setApprovalId(marketSmsAudit.getLevel3ApprovalId());
                        marketSmsAuditRecord.setApprovalName(marketSmsAudit.getLevel3ApprovalName());
                        marketSmsAuditRecordMapper.insert(marketSmsAuditRecord);
                    }

                }

            }
            // 在免审核情况下，如果预计运营规模大于200人，需要系统审核，且审核状态为待审核
            if (vo.getAuditType() == 2) {
                marketSmsStrategy.setExecuteStatus(3);
                marketSmsStrategy.setApprovalStatus(3);
            }
            if (vo.getAuditType() == 2 && vo.getSendNum() < 200) {
                b = false;
                marketSmsStrategy.setExecuteStatus(1);
                marketSmsStrategy.setApprovalStatus(3);
                marketSmsStrategy.setStartTime(TenMinutes(LocalDateTime.now()));
                marketSmsStrategy.setEndTime(OneYear(LocalDateTime.now()));
                this.update(marketSmsStrategy, new UpdateWrapper<MarketSmsStrategy>().lambda()
                        .eq(MarketSmsStrategy::getStrategyId, vo.getStrategyId()));
                sendToMegamarketAudit(marketSmsStrategy, marketSmsInfo);

            }
            if (b) {
                this.update(marketSmsStrategy, new UpdateWrapper<MarketSmsStrategy>().lambda()
                        .eq(MarketSmsStrategy::getStrategyId, vo.getStrategyId()));
            }
            res.setMsg("修改策略成功！");
            return res;
        } catch (MyException e) {
            log.error(vo.getStrategyName() + "的修改策略相应参数验证失败", e);
            throw e;
        } catch (Exception e) {
            log.error(vo.getStrategyName() + "的修改策略失败", e);
            throw new MyException(StatusEnum.ERROR, vo.getStrategyName() + "的修改策略失败");
        }

    }

    @Override
    public ResultObject revocation(String strategyId, String status) {
        ResultObject res = new ResultObject<>();

        UpdateWrapper<MarketSmsStrategy> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().set(MarketSmsStrategy::getExecuteStatus, status).eq(MarketSmsStrategy::getStrategyId,
                strategyId);

        if (status.equals("3") || status.equals("4") || status.equals("6")) {
            updateWrapper.lambda().set(MarketSmsStrategy::getApprovalStatus, 3);
        }

        marketSmsStrategyMapper.update(updateWrapper);
        if (status.equals("5")) {
            MarketSmsRevocationRecord marketSmsRevocationRecord = new MarketSmsRevocationRecord();
            Map<String, Object> params = AuthBaseUtils.authBaseInfo(null, request);
            marketSmsRevocationRecord.setStrategyId(strategyId);
            marketSmsRevocationRecord.setChId(params.get("creatorId").toString());
            marketSmsRevocationRecord.setChName(params.get("creatorName").toString());
            marketSmsRevocationRecord.setCpDepartment(params.get("creatorDepartment").toString());
            marketSmsRevocationRecord.setChTime(new Date());
            marketSmsRevocationRecordMapper.insert(marketSmsRevocationRecord);
        }

        res.setMsg("撤回成功");
        return res;
    }

    @Override
    public ResultObject strategyDetail(String strategyId) {
        ResultObject<MarketSmsStrategyDTO> res = new ResultObject<>();
        MarketSmsStrategyDTO marketSmsStrategyDTO = new MarketSmsStrategyDTO();
        MarketSmsStrategy marketSmsStrategy = marketSmsStrategyMapper.selectOne(
                new QueryWrapper<MarketSmsStrategy>().lambda().eq(true, MarketSmsStrategy::getStrategyId, strategyId));
        MarketSmsInfo marketSmsInfo = marketSmsInfoMapper
                .selectOne(new QueryWrapper<MarketSmsInfo>().lambda().eq(true, MarketSmsInfo::getStrategyId, strategyId));
        BeanUtils.copyProperties(marketSmsStrategy, marketSmsStrategyDTO);
        marketSmsStrategyDTO.setMarketSmsInfo(marketSmsInfo);
        res.setData(marketSmsStrategyDTO);
        return res;
    }

    @Override
    public ResultObject checkRecord(String strategyId) {
        ResultObject<MarketSmsAuditRecordDTO> res = new ResultObject<>();
        MarketSmsAuditRecordDTO marketSmsAuditRecordDTO = new MarketSmsAuditRecordDTO();
        List<UnifyAudit> unifyAudit = unifyAuditMapper
                .selectList(new QueryWrapper<UnifyAudit>().lambda().eq(UnifyAudit::getStrategyId, strategyId));
        List<UnifyAudit> collect = unifyAudit.stream().map(unifyAudit1 -> {
            if (FuncUtil.isNotEmpty(unifyAudit1.getLabel())) {
                unifyAudit1.setLabel(AuditStatus.getNameByCode(unifyAudit1.getLabel()));
            }
            if (FuncUtil.isNotEmpty(unifyAudit1.getStatus())) {
                unifyAudit1.setStatus(AuditStatus.getNameByCode(unifyAudit1.getStatus()));
            }
            return unifyAudit1;
        }).collect(Collectors.toList());
        List<MarketSmsAuditRecord> marketSmsAuditRecords = marketSmsAuditRecordMapper.selectList(
                new QueryWrapper<MarketSmsAuditRecord>().lambda().eq(MarketSmsAuditRecord::getStrategyId, strategyId));
        ArrayList<MarketSmsAuditRecord> marketSmsAuditRecordArrayList = new ArrayList<>();
        // 获取当前策略审核人数据
        MarketSmsAudit marketSmsAudit = marketSmsAuditMapper
                .selectOne(new QueryWrapper<MarketSmsAudit>().lambda().eq(MarketSmsAudit::getStrategyId, strategyId));

        if (!FuncUtil.isEmpty(marketSmsAudit)) {
            List<MarketSmsAuditRecord> marketSmsAuditRecords1 = marketSmsAuditRecords.stream()
                    .filter(marketSmsAuditRecord -> (FuncUtil.isNotNull(marketSmsAuditRecord.getApprovalId())
                            && marketSmsAuditRecord.getApprovalId().equals(marketSmsAudit.getLevel1ApprovalId()))
                            && (FuncUtil.isNotNull(marketSmsAuditRecord.getType()) && marketSmsAuditRecord.getType() == 1))
                    .collect(Collectors.toList());
            if (FuncUtil.isNotEmpty(marketSmsAudit.getLevel1ApprovalId())) {
                if (marketSmsAuditRecords1.size() != 0) {
                    marketSmsAuditRecords1.forEach(marketSmsAuditRecord -> {
                        MarketSmsAuditRecord marketSmsAuditRecord1 = new MarketSmsAuditRecord();
                        marketSmsAuditRecord1.setStrategyId(strategyId);
                        marketSmsAuditRecord1.setApprovalId(marketSmsAuditRecord.getApprovalId());
                        marketSmsAuditRecord1.setApprovalName(marketSmsAuditRecord.getApprovalName());
                        marketSmsAuditRecord1.setStatus(marketSmsAuditRecord.getStatus());
                        marketSmsAuditRecord1.setApprovalTime(marketSmsAuditRecord.getApprovalTime());
                        marketSmsAuditRecord1.setRemark(marketSmsAuditRecord.getRemark());
                        marketSmsAuditRecordArrayList.add(marketSmsAuditRecord1);
                    });
                } else {

                    MarketSmsAuditRecord marketSmsAuditRecord1 = new MarketSmsAuditRecord();
                    marketSmsAuditRecord1.setStrategyId(strategyId);
                    marketSmsAuditRecord1.setApprovalId(marketSmsAudit.getLevel1ApprovalId());
                    marketSmsAuditRecord1.setApprovalName(marketSmsAudit.getLevel1ApprovalName());
                    marketSmsAuditRecord1.setStatus(marketSmsAudit.getLevel1Status());
                    marketSmsAuditRecordArrayList.add(marketSmsAuditRecord1);
                }

            }

            List<MarketSmsAuditRecord> marketSmsAuditRecords2 = marketSmsAuditRecords.stream()
                    .filter(marketSmsAuditRecord -> (FuncUtil.isNotNull(marketSmsAuditRecord.getApprovalId())
                            && marketSmsAuditRecord.getApprovalId().equals(marketSmsAudit.getLevel2ApprovalId()))
                            && (FuncUtil.isNotNull(marketSmsAuditRecord.getType()) && marketSmsAuditRecord.getType() == 2))
                    .collect(Collectors.toList());

            if (FuncUtil.isNotEmpty(marketSmsAudit.getLevel2ApprovalId())) {
                if (marketSmsAuditRecords2.size() != 0) {
                    marketSmsAuditRecords2.forEach(marketSmsAuditRecord -> {
                        MarketSmsAuditRecord marketSmsAuditRecord2 = new MarketSmsAuditRecord();
                        marketSmsAuditRecord2.setStrategyId(strategyId);
                        marketSmsAuditRecord2.setApprovalId(marketSmsAuditRecord.getApprovalId());
                        marketSmsAuditRecord2.setApprovalName(marketSmsAuditRecord.getApprovalName());
                        marketSmsAuditRecord2.setStatus(marketSmsAuditRecord.getStatus());
                        marketSmsAuditRecord2.setApprovalTime(marketSmsAuditRecord.getApprovalTime());
                        marketSmsAuditRecord2.setRemark(marketSmsAuditRecord.getRemark());
                        marketSmsAuditRecordArrayList.add(marketSmsAuditRecord2);
                    });
                } else {
                    MarketSmsAuditRecord marketSmsAuditRecord2 = new MarketSmsAuditRecord();
                    marketSmsAuditRecord2.setStrategyId(strategyId);
                    marketSmsAuditRecord2.setApprovalId(marketSmsAudit.getLevel2ApprovalId());
                    marketSmsAuditRecord2.setApprovalName(marketSmsAudit.getLevel2ApprovalName());
                    marketSmsAuditRecord2.setStatus(marketSmsAudit.getLevel2Status());
                    marketSmsAuditRecordArrayList.add(marketSmsAuditRecord2);
                }
            }

            List<MarketSmsAuditRecord> marketSmsAuditRecords3 = marketSmsAuditRecords.stream()
                    .filter(marketSmsAuditRecord -> (FuncUtil.isNotNull(marketSmsAuditRecord.getApprovalId())
                            && marketSmsAuditRecord.getApprovalId().equals(marketSmsAudit.getLevel3ApprovalId()))
                            && (FuncUtil.isNotNull(marketSmsAuditRecord.getType()) && marketSmsAuditRecord.getType() == 3))
                    .collect(Collectors.toList());

            if (FuncUtil.isNotEmpty(marketSmsAudit.getLevel3ApprovalId())) {
                if (marketSmsAuditRecords3.size() != 0) {
                    marketSmsAuditRecords3.forEach(marketSmsAuditRecord -> {
                        MarketSmsAuditRecord marketSmsAuditRecord3 = new MarketSmsAuditRecord();
                        marketSmsAuditRecord3.setStrategyId(strategyId);
                        marketSmsAuditRecord3.setApprovalId(marketSmsAuditRecord.getApprovalId());
                        marketSmsAuditRecord3.setApprovalName(marketSmsAuditRecord.getApprovalName());
                        marketSmsAuditRecord3.setStatus(marketSmsAuditRecord.getStatus());
                        marketSmsAuditRecord3.setApprovalTime(marketSmsAuditRecord.getApprovalTime());
                        marketSmsAuditRecord3.setRemark(marketSmsAuditRecord.getRemark());
                        marketSmsAuditRecordArrayList.add(marketSmsAuditRecord3);
                    });
                } else {
                    MarketSmsAuditRecord marketSmsAuditRecord3 = new MarketSmsAuditRecord();
                    marketSmsAuditRecord3.setStrategyId(strategyId);
                    marketSmsAuditRecord3.setApprovalId(marketSmsAudit.getLevel3ApprovalId());
                    marketSmsAuditRecord3.setApprovalName(marketSmsAudit.getLevel3ApprovalName());
                    marketSmsAuditRecord3.setStatus(marketSmsAudit.getLevel3Status());
                    marketSmsAuditRecordArrayList.add(marketSmsAuditRecord3);
                }
            }

        }
        marketSmsAuditRecordDTO.setUnifyAudit(collect);
        marketSmsAuditRecordDTO.setSmsAuditRecordList(marketSmsAuditRecordArrayList);
        res.setData(marketSmsAuditRecordDTO);
        return res;
    }

    @Override
    public ResultObject revocationRecord(String strategyId) {
        ResultObject res = new ResultObject<>();
        List<MarketSmsRevocationRecord> revocationRecordList =
                marketSmsRevocationRecordMapper.selectList(new QueryWrapper<MarketSmsRevocationRecord>().lambda()
                        .eq(MarketSmsRevocationRecord::getStrategyId, strategyId));
        res.setData(revocationRecordList);
        return res;
    }

    @Override
    public ResultObject delete(String strategyId) {
        ResultObject res = new ResultObject<>();
        marketSmsAuditMapper
                .delete(new QueryWrapper<MarketSmsAudit>().lambda().eq(MarketSmsAudit::getStrategyId, strategyId));
        marketSmsAuditRecordMapper.delete(
                new QueryWrapper<MarketSmsAuditRecord>().lambda().eq(MarketSmsAuditRecord::getStrategyId, strategyId));
        marketSmsInfoMapper
                .delete(new QueryWrapper<MarketSmsInfo>().lambda().eq(MarketSmsInfo::getStrategyId, strategyId));
        marketSmsRevocationRecordMapper.delete(new QueryWrapper<MarketSmsRevocationRecord>().lambda()
                .eq(MarketSmsRevocationRecord::getStrategyId, strategyId));
        marketSmsStrategyMapper
                .delete(new QueryWrapper<MarketSmsStrategy>().lambda().eq(MarketSmsStrategy::getStrategyId, strategyId));
        res.setMsg("删除成功");
        return res;
    }

    @Override
    public ResultObject checkList(MarketSmsAuditPageVO vo) {
        ResultObject<Page<MarketSmsStrategy>> res = new ResultObject<>();
        try {
            SysUser user = AuthBaseUtils.getSessionUser(request);
            // 超级管理员可以查询所有运营策略， 非超级管理员仅能查询本部门运营策略
            String userID = user.getId().toString();
            List<String> roles = roleMapper.queryRoleIdByUserId(userID);
            if (!roles.contains(RoleConstants.AUDITOR) && !roles.contains(RoleConstants.SUPER_ADMIN)
                    && !roles.contains(RoleConstants.SUPERAIMIN)) {
                // 如果不是系统管理员或超级管理员或短信审核人， 设置查询范围为本部门
                vo.setApprovalDepartment(user.getDeptName());
            }
            QueryWrapper<MarketSmsStrategy> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(FuncUtil.isNotEmpty(vo.getApprovalDepartment()), MarketSmsStrategy::getCreatorDepartment,
                            vo.getApprovalDepartment())
                    .like(FuncUtil.isNotEmpty(vo.getApprovalName()), MarketSmsStrategy::getCreatorName,
                            vo.getApprovalName())
                    .like(FuncUtil.isNotEmpty(vo.getStrategyName()), MarketSmsStrategy::getStrategyName,
                            vo.getStrategyName())
                    .eq(FuncUtil.isNotNull(vo.getApprovalStatus()), MarketSmsStrategy::getApprovalStatus,
                            vo.getApprovalStatus())
                    .eq(FuncUtil.isNotEmpty(vo.getStrategyId()), MarketSmsStrategy::getStrategyId, vo.getStrategyId())
                    .ge(FuncUtil.isNotEmpty(vo.getSd()), MarketSmsStrategy::getCreateTime, vo.getSd())
                    .le(FuncUtil.isNotEmpty(vo.getEd()), MarketSmsStrategy::getCreateTime, vo.getEd())
                    .notIn(MarketSmsStrategy::getExecuteStatus, 5).orderByDesc(MarketSmsStrategy::getCreateTime);
            // 分页查询结果集
            Long total = marketSmsStrategyMapper.selectCount(queryWrapper);
            Page<MarketSmsStrategy> pageQuery = new Page<>(vo.getPage(), vo.getPageSize());
            IPage<MarketSmsStrategy> result = baseMapper.selectPage(pageQuery, queryWrapper);
            List<MarketSmsStrategySAuditDTO> mapList = result.getRecords().stream().map(marketSmsStrategy -> {
                MarketSmsStrategySAuditDTO marketSmsStrategySAuditDTO = new MarketSmsStrategySAuditDTO();
                BeanUtils.copyProperties(marketSmsStrategy, marketSmsStrategySAuditDTO);
                MarketSmsAudit marketSmsAudit = marketSmsAuditMapper.selectOne(new QueryWrapper<MarketSmsAudit>()
                        .lambda().eq(MarketSmsAudit::getStrategyId, marketSmsStrategy.getStrategyId()));
                marketSmsStrategySAuditDTO.setMarketSmsAudit(marketSmsAudit);
                if (marketSmsStrategy.getAuditType() == 1 && marketSmsStrategy.getSendNum() > 200) {
                    if (!FuncUtil.isEmpty(marketSmsAudit)) {
                        // 0短信审核
                        if (marketSmsAudit.getLevel1Status() == 3) {
                            marketSmsStrategySAuditDTO.setStatus(0);
                        } else {
                            marketSmsStrategySAuditDTO.setStatus(1);
                        }
                    }
                }
                if (marketSmsStrategy.getAuditType() == 1 && marketSmsStrategy.getSendNum() < 200) {
                    marketSmsStrategySAuditDTO.setStatus(0);
                }
                if (marketSmsStrategy.getAuditType() == 2 && marketSmsStrategy.getSendNum() > 200) {
                    marketSmsStrategySAuditDTO.setStatus(1);
                }
                if (marketSmsStrategy.getAuditType() == 2 && marketSmsStrategy.getSendNum() < 200) {
                    marketSmsStrategySAuditDTO.setStatus(2);
                }
                return marketSmsStrategySAuditDTO;
            }).collect(Collectors.toList());
            // 分页
            Page page = new Page<>();
            page.setTotal(total);
            page.setRecords(mapList);
            res.setCode(StatusEnum.SUCCESS.getCode());
            res.setMsg(StatusEnum.SUCCESS.getMsg());
            res.setData(page);
            return res;
        } catch (Exception e) {
            log.error("查询策略列表失败", e);
            throw new MyException(StatusEnum.ERROR, "查询策略列表失败");
        }
    }

    @Override
    public ResultObject effectList(MarketSmsCountPageVO vo) {
        ResultObject res = new ResultObject<>();
        Page page = new Page<>(vo.getPage(), vo.getPageSize());
        IPage<MarketSmsCountDTO> iPage = marketSmsStrategyMapper.effectListPage(page, vo);
        res.setData(iPage);
        return res;
    }

    @Override
    public ResultObject check(MarketSmsAuditRecord vo) {
        boolean b = true;
        // 获取当前策略审核人数据
        MarketSmsAudit marketSmsAudit = marketSmsAuditMapper.selectOne(
                new QueryWrapper<MarketSmsAudit>().lambda().eq(MarketSmsAudit::getStrategyId, vo.getStrategyId()));
        MarketSmsAudit audit = new MarketSmsAudit();
        BeanUtils.copyProperties(marketSmsAudit, audit);
        // 获取策略内容
        MarketSmsStrategy marketSmsStrategy = marketSmsStrategyMapper.selectOne(new QueryWrapper<MarketSmsStrategy>()
                .lambda().eq(true, MarketSmsStrategy::getStrategyId, vo.getStrategyId()));
        UpdateWrapper<MarketSmsStrategy> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(MarketSmsStrategy::getStrategyId, vo.getStrategyId());

        // 1級 审批
        if (marketSmsAudit.getLevel1Status() == 0 && marketSmsAudit.getLevel1ApprovalId().equals(vo.getApprovalId())) {
            vo.setType(1);
            // 判断 审核状态 通过
            if (vo.getStatus().equals(1)) {
                audit.setLevel1Status(1);
                audit.setLevel2Status(0);

            }
            // 判断 审核状态 拒绝
            if (vo.getStatus().equals(2)) {
                audit.setLevel1Status(2);
                marketSmsStrategy.setExecuteStatus(2);
                marketSmsStrategy.setApprovalStatus(2);
            }
            marketSmsStrategyMapper.update(marketSmsStrategy, updateWrapper);
        }

        // 2級 审批
        if (marketSmsAudit.getLevel2Status() == 0 && marketSmsAudit.getLevel2ApprovalId().equals(vo.getApprovalId())) {
            vo.setType(2);
            // 判断 审核状态 通过
            if (vo.getStatus().equals(1)) {
                audit.setLevel2Status(1);
                if (FuncUtil.isNotEmpty(audit.getLevel3ApprovalId())) {
                    audit.setLevel3Status(0);
                } else {
                    b = false;
                    marketSmsStrategy.setExecuteStatus(1);
                    marketSmsStrategy.setApprovalStatus(3);
                    marketSmsStrategy.setStartTime(TenMinutes(LocalDateTime.now()));
                    marketSmsStrategy.setEndTime(OneYear(LocalDateTime.now()));
                    marketSmsStrategyMapper.update(marketSmsStrategy, updateWrapper);
                    // 获取短信容
                    MarketSmsInfo marketSmsInfo = marketSmsInfoMapper.selectOne(new QueryWrapper<MarketSmsInfo>()
                            .lambda().eq(true, MarketSmsInfo::getStrategyId, vo.getStrategyId()));
                    // 发起大营销平台审核
                    try {
                        sendToMegamarketAudit(marketSmsStrategy, marketSmsInfo);

                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
            // 判断 审核状态 拒绝
            if (vo.getStatus().equals(2)) {
                audit.setLevel2Status(2);
                marketSmsStrategy.setExecuteStatus(2);
                marketSmsStrategy.setApprovalStatus(2);
            }
            marketSmsStrategyMapper.update(marketSmsStrategy, updateWrapper);
        }

        // 3級 审批
        if (marketSmsAudit.getLevel3Status() == 0 && marketSmsAudit.getLevel3ApprovalId().equals(vo.getApprovalId())) {
            vo.setType(3);
            // 判断 审核状态 通过
            if (vo.getStatus().equals(1)) {
                b = false;
                audit.setLevel3Status(1);
                marketSmsStrategy.setExecuteStatus(1);
                marketSmsStrategy.setApprovalStatus(3);
                marketSmsStrategy.setStartTime(TenMinutes(LocalDateTime.now()));
                marketSmsStrategy.setEndTime(OneYear(LocalDateTime.now()));
                marketSmsStrategyMapper.update(marketSmsStrategy, updateWrapper);
                // 获取短信容
                MarketSmsInfo marketSmsInfo = marketSmsInfoMapper.selectOne(new QueryWrapper<MarketSmsInfo>().lambda()
                        .eq(true, MarketSmsInfo::getStrategyId, vo.getStrategyId()));
                // 发起大营销平台审核
                try {
                    sendToMegamarketAudit(marketSmsStrategy, marketSmsInfo);

                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            // 判断 审核状态 拒绝
            if (vo.getStatus().equals(2)) {
                audit.setLevel3Status(2);
                marketSmsStrategy.setExecuteStatus(2);
                marketSmsStrategy.setApprovalStatus(2);
            }
            // 推送大營銷則不再此修改狀態避免狀態重複
            if (b) {
                marketSmsStrategyMapper.update(marketSmsStrategy, updateWrapper);
            }
        }

        vo.setApprovalTime(DateUtils.formatDate(new Date(), DateUtils.PATTERN_ASCTIME));
        // 更新审核状态
        UpdateWrapper<MarketSmsAudit> smsAuditUpdateWrapper = new UpdateWrapper<>();
        smsAuditUpdateWrapper.lambda().eq(true, MarketSmsAudit::getStrategyId, vo.getStrategyId());
        marketSmsAuditMapper.update(audit, smsAuditUpdateWrapper);
        vo.setApprovalTime(com.ebupt.migu.common.util.DateUtil.getDateNow());
        QueryWrapper<MarketSmsAuditRecord> queryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<MarketSmsAuditRecord> aNull = queryWrapper.lambda()
                .eq(MarketSmsAuditRecord::getStrategyId, vo.getStrategyId()).isNull(MarketSmsAuditRecord::getStatus);
        List<MarketSmsAuditRecord> marketSmsAuditRecords = marketSmsAuditRecordMapper.selectList(aNull);
        if (marketSmsAuditRecords.size() > 0) {
            aNull.eq(MarketSmsAuditRecord::getApprovalId, vo.getApprovalId());
            marketSmsAuditRecordMapper.update(vo, aNull);
        } else {
            marketSmsAuditRecordMapper.insert(vo);
        }
        ResultObject res = new ResultObject();
        res.setMsg("操作成功！");
        return res;
    }


    @Override
    public List<MarketSmsCountDTO> export(MarketSmsCountPageVO params) {
        Page page = new Page<>(params.getPage(), params.getPageSize());
        IPage<MarketSmsCountDTO> iPage = marketSmsStrategyMapper.export(page, params);
        return iPage.getRecords();
    }

    @Override
    public ResultObject checkProgress(String strategyId) {
        LambdaQueryWrapper<MarketSmsStrategy> strategyWrapper = new LambdaQueryWrapper<>();
        strategyWrapper.eq(MarketSmsStrategy::getStrategyId, strategyId);
        MarketSmsStrategy marketSmsStrategy = marketSmsStrategyMapper.selectOne(strategyWrapper);
        LambdaQueryWrapper<MarketSmsCount> countWrapper = new LambdaQueryWrapper<>();
        countWrapper.eq(MarketSmsCount::getStrategyId, strategyId);
        MarketSmsCount marketSmsCount = marketSmsCountMapper.selectOne(countWrapper);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("count", marketSmsCount.getCount());
        hashMap.put("sendNum", marketSmsStrategy.getSendNum());
        return ResultObject.ok(hashMap);
    }

    /**
     * 将短信策略发送给大营销平台审核
     *
     * @param marketSmsStrategy
     */
    public boolean sendToMegamarketAudit(MarketSmsStrategy marketSmsStrategy, MarketSmsInfo marketSmsInfo)
            throws IOException {
        MiguSubCompanyCampDto miguSubCompanyCampDto = new MiguSubCompanyCampDto();
        miguSubCompanyCampDto.setBlackUser(1);
        miguSubCompanyCampDto.setCampsegDesc(marketSmsStrategy.getRemark() == null ? "1"
                : StringUtils.isNotBlank(marketSmsStrategy.getRemark()) ? marketSmsStrategy.getRemark() : "1");
        miguSubCompanyCampDto.setCampsegId(marketSmsStrategy.getStrategyId());
        miguSubCompanyCampDto.setCampsegName(marketSmsStrategy.getStrategyName());
        // 父id，要与策略id不一致
        String campsegPid = marketSmsStrategy.getStrategyId() + (int) ((Math.random() * 9 + 1) * Math.pow(10, 4 - 1));
        miguSubCompanyCampDto.setCampsegPid(campsegPid);
        // 使用服务云自己的用户id
        miguSubCompanyCampDto.setCreateLoginName(marketSmsStrategy.getCreatorId());
        // 短信下发去重天数，暂时不用去重
        miguSubCompanyCampDto.setDistictDays(0);

        // TODO 上线前需要确认发送到大营销平台的当前活动有效期
        String startDate = DateUtil.getDateAfter10MinutesAsString();
        String endDate = DateUtil.getDateAfter1YearAsString();
        miguSubCompanyCampDto.setEndDate(endDate);
        miguSubCompanyCampDto.setStartDate(startDate);
        HashMap<String, String> megamarket =
                megamarketMapper.qryMegamarketInfoByID(Integer.valueOf(marketSmsStrategy.getSendPort()));
        miguSubCompanyCampDto.setAdivId(megamarket.get("adivId"));
        // 短信内容不能超过100
        miguSubCompanyCampDto.setSmsContent(marketSmsInfo.getSmsContent().length() >= 100
                ? marketSmsInfo.getSmsContent().substring(0, 99) : marketSmsInfo.getSmsContent());
        miguSubCompanyCampDto.setSubCompanyCode(StrategyConstants.MEGAMARKET_SUBCOMPANYCODE);
        // 大营销账户组编码，由大营销平台定义
        miguSubCompanyCampDto.setUserGroupId(StrategyConstants.MEGAMARKET_USERGROUPID);
        miguSubCompanyCampDto.setCampsegStatId(StrategyConstants.MEGAMARKET_CAMPSEGSTATID_CHECKING);

        log.info("{}发送的body miguSubCompanyCampDto:{}", marketSmsStrategy.getStrategyId(),
                miguSubCompanyCampDto.toString());

        String url = MEGAMARKET_IP + MEGAMARKET_SUBCOMPANYCAMPSYNC2MARKETING;

        log.info("服务端URL: {}", url);

        String response = HttpUtils.doPost(url, JsonUtils.obj2Json(miguSubCompanyCampDto), null);
        log.info("HTTP请求结果：{}", response);
        Map responseMap = JsonUtils.json2Obj(response, HashMap.class);
        if (!responseMap.get("status").toString().equals("SUCCESS")) {
            try {
                String[] tels = new String[1];
                tels[0] = String.valueOf(marketSmsStrategy.getCreatorPhone());
                String[] vals = new String[3];
                vals[0] = String.valueOf(marketSmsStrategy.getStrategyName());
                vals[1] = "短信";
                vals[2] = "大营销平台默认审核失败";
                Sender.sendSMS("20100066", tels, vals);
                return true;
            } catch (Exception e) {
                log.error("发送通知短信失败：{} 内容: {}", marketSmsStrategy, marketSmsInfo);
                throw new MyException(StatusEnum.SMS_SEND_MEGAMARKET_AUDIT, "同步到大营销平台接口调用异常");
            }
        }
        return false;
    }

    /**
     * 离线短信统一审核能力接入
     *
     * @param marketSmsInfo
     */
    private void smsUnifyAudit(MarketSmsInfo marketSmsInfo) {
        String dataId = marketSmsInfo.getStrategyId() + "-"
                + DateUtil.parseDateToStr(new Date(), DateUtil.DATE_TIME_FORMAT_YYYYMMDDHHMISS);
        AuditData data = AuditData.builder().dataId(dataId).data(marketSmsInfo.getSmsContent()).dataType("短信内容")
                .description("").priority("HIGH").build();
        unifyAuditService.auditSend(marketSmsInfo.getStrategyId().toString(), Collections.singletonList(data));
    }

    /**
     * 生成策略Id
     *
     * @return
     */
    private String getStrategyId() {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        LocalDateTime time = LocalDateTime.now();
        return "MS".concat(df.format(time));
    }

    public String TenMinutes(LocalDateTime currentTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 当前时间加 10 分钟
        LocalDateTime plusTenMinutes = currentTime.plusMinutes(10);
        String st = plusTenMinutes.format(formatter);
        System.out.println("当前时间加 10 分钟：" + st);
        return st;
    }

    public String OneYear(LocalDateTime currentTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 当前时间加一年
        LocalDateTime plusOneYear = currentTime.plus(1, ChronoUnit.YEARS);
        String et = plusOneYear.format(formatter);
        System.out.println("当前时间加一年：" + et);
        return et;
    }


}
